// src/routes/launches/launches.controller.js
const {
    getAllLaunches,
    addNewLaunch,
    existsLaunchWithId,
    abortLaunchById,
    } = require('../../models/launches.model');
    function httpGetAllLaunches(req, res) {
    return res.status(200).json(getAllLaunches());
    }
    function httpAddNewLaunch(req, res) {
    const launch = req.body;
    // 检查请求体中是否包含所需的属性
    if (!launch.mission || !launch.rocket || !launch.launchDate
    || !launch.target) {
    return res.status(400).json({
    error: '缺少必要的发射属性',
    });
    }
    launch.launchDate = new Date(launch.launchDate);
    // 检查发射⽇期是否有效
    if (isNaN(launch.launchDate)) {
        return res.status(400).json({
            error: '发射⽇期⽆效',
            });
            }
            addNewLaunch(launch);
            return res.status(201).json(launch);
            }

            function httpAbortLaunch(req, res) {
                const launchId = Number(req.params.id);
                if (!existsLaunchWithId(launchId)) {
                return res.status(404).json({
                error: '发射任务不存在',
                });
                }
                const aborted = abortLaunchById(launchId);
                return res.status(200).json(aborted);
               }
               module.exports = {
                httpGetAllLaunches,
                httpAddNewLaunch,
                httpAbortLaunch,
               };